<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>乐优商城--购物车页面</title>

    <link rel="stylesheet" type="text/css" href="css/webbase.css"/>
    <link rel="stylesheet" type="text/css" href="css/pages-cart.css"/>
</head>

<body>

<div id="cartApp">

    <div class="top">
        <shortcut/>
    </div>

    <div class="cart py-container">

        <!--logoArea-->
        <div class="logoArea">
            <div class="fl logo"><span class="title">购物车</span></div>
        </div>

        <!--All goods-->
        <div class="allgoods">
            <h4>全部商品<span>11</span></h4>
            <div class="cart-main">
                <div class="yui3-g cart-th">
                    <div class="yui3-u-1-4"><input type="checkbox"  v-model="isSelectAll" @click="changeSelected" /> 全部</div>
                    <div class="yui3-u-1-4">商品</div>
                    <div class="yui3-u-1-8">单价（元）</div>
                    <div class="yui3-u-1-8">数量</div>
                    <div class="yui3-u-1-8">小计（元）</div>
                    <div class="yui3-u-1-8">操作</div>
                </div>
                <div class="cart-item-list">

                    <div class="cart-body">
                        <div class="cart-list">
                            <ul class="goods-list yui3-g" v-for="(c,i) in carts" :key="i" v-if="c.enable">
                                <li class="yui3-u-1-24">
                                    <input type="checkbox" v-model="selected" name="" :value="c"/>
                                </li>
                                <li class="yui3-u-11-24">
                                    <div class="good-item">
                                        <div class="item-img"><img :src="c.image" width="80px" height="80px"/></div>
                                        <div class="item-msg">
												<span style="line-height:70px ">
													{{c.title}}<br>
                                                    <span v-for="(v,k) in   c.ownSpec"
                                                          :key="k">{{c.paramMap[k]}}:{{v}}</span><br>
												</span>
                                        </div>
                                    </div>
                                </li>

                                <li class="yui3-u-1-8">
                                  <span style="line-height:70px " class="price">商品价格:{{ly.formatPrice(c.skuPrice)}}</span>
                                    <br><span v-if="c.skuPrice!=c.price" >加入时价格{{ly.formatPrice(c.skuPrice)}}</span>
                                  <span v-if="c.skuPrice!=c.price"> <br>商品{{c.skuPrice>c.price?'涨价:'+ly.formatPrice(c.skuPrice-c.price):'降价:'+ly.formatPrice(c.price-c.skuPrice)}}元</span>
                                </li>
                                <li class="yui3-u-1-8" style="padding-top: 20px">
                                    <a href="javascript:void(0)" class="increment mins" @click="decrement(c)">-</a>
                                    <input autocomplete="off" type="text" v-model="c.num" value="1" minnum="1"
                                           class="itxt"/>
                                    <a href="javascript:void(0)" class="increment plus" @click="increment(c)">+</a>
                                </li>
                                <li class="yui3-u-1-8"><span style="line-height:70px " class="sum">{{ly.formatPrice(c.price*c.num)}}</span>
                                </li>
                                <li class="yui3-u-1-8">
                                    <a href="#none" @click="deleteCart(c)">删除</a><br/>
                                    <a href="#none" @click="moveToCollections(c)">移到我的关注</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="cart-body" v-if="unShelveCarts.length>0">
                        <p1>下架商品</p1>
                        <br>
                        <!--已经下架的商品-->
                        <div class="cart-list">
                            <ul class="goods-list yui3-g" v-for="(c,i) in unShelveCarts" :key="i">
                                <li class="yui3-u-11-24">
                                    <div class="good-item">
                                        <div class="item-img"><img :src="c.image" width="80px" height="80px"/></div>
                                        <div class="item-msg">
												<span style="line-height:70px ">
													{{c.title}}<br>
                                                    <span v-for="(v,k) in   c.ownSpec"
                                                          :key="k">{{c.paramMap[k]}}:{{v}}</span><br>
												</span>
                                        </div>
                                    </div>
                                </li>

                                <li class="yui3-u-1-8">
                                  <span v-if="c.skuPrice!=c.price">商品价格</span>  <span style="line-height:70px " class="price">{{ly.formatPrice(c.skuPrice)}}</span>
                                </li>
                                <li class="yui3-u-1-8" style="padding-top: 20px">
                                    <!--<a href="javascript:void(0)" class="increment mins" @click="decrement(c)">-</a>-->
                                    <input autocomplete="off" type="text" v-model="c.num" value="1" minnum="1" disabled
                                           class="itxt"/>
                                    <!--<a href="javascript:void(0)" class="increment plus" @click="increment(c)">+</a>-->
                                </li>
                                <li class="yui3-u-1-8"><span style="line-height:70px " class="sum">{{ly.formatPrice(c.skuPrice*c.num)}}</span>
                                </li>
                                <li class="yui3-u-1-8">
                                    <a href="#none" @click="deleteCart(c)">删除</a><br/>
                                    <a href="#none">移到我的关注</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>

            </div>
            <div class="cart-tool">
                <div class="select-all">
                    <input type="checkbox" name="" id="" value="" v-model="isSelectAll" @click="changeSelected"/>
                    <span>全选</span>
                </div>
                <div class="option">
                    <a href="#none">删除选中的商品</a>
                    <a href="#none">移到我的关注</a>
                    <a href="#none">清除下柜商品</a>
                </div>
                <div class="toolbar">
                    <div class="chosed">已选择<span>0</span>件商品</div>
                    <div class="sumprice">
                        <span><em>总价（不含运费） ：</em><i class="summoney">{{ly.formatPrice(totalPrice)}}</i></span>
                        <span><em>已节省：</em><i>-¥20.00</i></span>
                    </div>
                    <div class="sumbtn">
                        <a class="sum-btn" href="#" @click="toOrderInfo">结算</a>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="deled">
                <span>已删除商品，您可以重新购买或加关注：</span>
                <div class="cart-list del" v-for="(cart,index) in deleteCarts" :key="index">
                    <ul class="goods-list yui3-g">
                        <li class="yui3-u-1-2">
                            <div class="good-item">
                                <div class="item-msg" v-text="cart.title">Apple Macbook Air 13.3英寸笔记本电脑 银色（Corei5）处理器/8GB内存</div>
                            </div>
                        </li>
                        <li class="yui3-u-1-6"><span class="price">{{ly.formatPrice(cart.skuPrice)}}</span></li>
                        <li class="yui3-u-1-6">
                            <span class="number">{{cart.num}}</span>
                        </li>
                        <li class="yui3-u-1-8">
                            <a href="#none"  @click.prevent="moveToCarts(cart)">重新购买</a>
                            <a href="#none" @click.prevent="moveToCollections(cart)">移到我的关注</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="liked">
                <ul class="sui-nav nav-tabs">
                    <li class="active">
                        <a href="#index" data-toggle="tab">猜你喜欢</a>
                    </li>
                    <li>
                        <a href="#profile" data-toggle="tab">特惠换购</a>
                    </li>
                </ul>
                <div class="clearfix"></div>
                <div class="tab-content">
                    <div id="index" class="tab-pane active">
                        <div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
                            <div class="carousel-inner">
                                <div class="active item">
                                    <ul>
                                        <li>
                                            <img src="img/like1.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like2.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like3.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like4.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                                <div class="item">
                                    <ul>
                                        <li>
                                            <img src="img/like1.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like2.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like3.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like4.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
                            <a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
                        </div>
                    </div>
                    <div id="profile" class="tab-pane">
                        <p>特惠选购</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
<script src="./js/vue/vue.js"></script>
<script src="./js/axios.min.js"></script>
<script src="./js/common.js"></script>
<script type="text/javascript">
    var cartVm = new Vue({
        el: "#cartApp",
        data: {
            ly,
            carts: [],
            selected: [1,2,3,4],
            selectAll: true,
            deleteCarts:[],
        },
        mounted() {
            //加载数据
            this.loadCarts();
            //先合并购物车
            this.combineCarts();
            this.deleteCarts = ly.sessionStore.get("deleteCarts") || [];
        },
        computed: {
            totalPrice() {
                return this.selected.map(c => c.num * c.skuPrice).reduce((n1, n2) => n1 + n2, 0);
            },
            //下架商品的计算属性
            unShelveCarts() {
               return  this.carts.filter(c => !c.enable);
            },
            isSelectAll() {
                return this.selected.length === this.carts.length;
            },
        },
        methods: {
            changeSelected() {
                this.selectAll = this.isSelectAll;
                if (this.selectAll) {
                    //现在是全选,点击的话应该是清空
                    this.selected = [];
                } else {
                    //本来不是全选的话就全选
                       this.selected = [];
                    this.carts.forEach(c =>this.selected.push(c));
                }
            },
            /**
             * 将删除的数据移动到购物车
             * @param cart
             */
            moveToCarts(cart) {
                ly.verifyUser().then(()=>{
                    //将数据传到后台
                    //从后台取数据
                    ly.http.post("/cart", cart).then(resp => {
                       //后台添加成功后将数据添加到carts中
                        this.carts.push(cart);
                        let  index=this.deleteCarts.findIndex(c => c.skuId === cart.skuId);
                        this.deleteCarts.splice(index, 1);
                        ly.sessionStore.set("deleteCarts", this.deleteCarts);
                    });
                }).catch(()=>{
                    //未登录的话
                    this.carts.push(cart);
                    ly.store.set("carts", this.carts);
                   let  index=this.deleteCarts.findIndex(c => c.skuId === cart.skuId);
                    this.deleteCarts.splice(index, 1);
                    ly.sessionStore.set("deleteCarts", this.deleteCarts);
                })
            },
            toOrderInfo() {
                ly.verifyUser().then(
                    () => {

                        //将购物车的数据添加到付款页面
                        ly.store.set("selectedCarts", this.selected);


                        location.href = "/getOrderInfo.html";
                    }
                ).catch(() => {
                    //未登录
                    window.location.href = "http://www.leyou.com/login.html?returnUrl=" + window.location.href
                });
            },
            combineCarts() {
                ly.verifyUser().then(
                    () => {
                        //登陆的话
                        let carts = ly.store.get("carts");
                        if (carts && carts.length > 0) {
                            ly.http.post("/cart/combine", carts).then(
                                //添加成功的话清楚本地的缓存
                                () => {
                                    ly.store.del("carts");
                                    this.loadCarts();
                                }
                            );
                        }
                    }
                ).catch(
                    () => {
                        console.log("合并购物车失败");
                    }
                );
            },
            //加载购物车数据到本地
            loadCarts() {
                ly.verifyUser().then(
                    () => {
                        //从后台取数据
                        ly.http.get("/cart").then(resp => {
                            this.carts = resp.data;
                            this.selected = resp.data.filter(c=>c.enable);
                        });
                    }
                ).catch(
                    () => {
                        //从前台取数据
                        this.carts = ly.store.get("carts") || [];
                        this.selected = ly.store.get("carts") || [];
                    }
                );
            },
            increment(ct) {

                ly.verifyUser().then(
                    () => {
                        //从后台取数据
                        ly.http.put("/cart", ly.stringify({skuId: ct.skuId, num: ct.num + 1})).then(resp => {
                            ct.num++;
                        });
                    }
                ).catch(
                    () => {
                        ct.num++;
                        //把数据存到localstorege
                        ly.store.set("carts", this.carts);
                    }
                );
            },
            decrement(ct) {
                ly.verifyUser().then(
                    () => {
                        //从后台取数据
                        ly.http.put("/cart", ly.stringify({skuId: ct.skuId, num: ct.num + 1})).then(resp => {
                            if (ct.num > 1) {
                                ct.num--;
                            }
                        });
                    }
                ).catch(
                    () => {
                        if (ct.num > 1) {
                            ct.num--;
                        }
                        //把数据存到localstorege
                        ly.store.set("carts", this.carts);
                    }
                );
            },
            moveToCollections(ct) {
                ly.verifyUser().then(()=>{
                    ly.http.post("/user/collections",ly.stringify({skuId:ct.skuId})).then(()=>{
                        //删除成功后,删除页面的标签
                        let index = this.carts.findIndex(cart => cart.skuId === ct.skuId);
                        this.carts.splice(index, 1);
                    }).catch()
                });
            },
            deleteCart(ct) {
                let cartsIndex = this.carts.findIndex(c => c.skuId === ct.skuId);
                let selectedIndex = this.selected.findIndex(c => c.skuId === ct.skuId);
                ly.verifyUser().then(
                    () => {
                        //从后台取数据
                        ly.http.delete("/cart/" + ct.skuId).then(
                            () => {
                                 const  ct=this.carts[cartsIndex];
                                this.carts.splice(cartsIndex, 1);
                                //将deletecart
                                this.deleteCarts= ly.sessionStore.get("deleteCarts")|| [];
                                this.deleteCarts.push(ct);
                                ly.sessionStore.set("deleteCarts", this.deleteCarts);
                                if (selectedIndex) {
                                    this.selected.splice(selectedIndex, 1);
                                }
                            }
                        ).catch(() => alert("删除服务器失败"));
                    }
                ).catch(
                    () => {
                        const  ct=this.carts[cartsIndex];
                        this.carts.splice(cartsIndex, 1);
                        //将deletecart
                        this.deleteCarts= ly.sessionStore.get("deleteCarts")|| [];
                        this.deleteCarts.push(ct);
                        ly.sessionStore.set("deleteCarts", this.deleteCarts);
                        if (selectedIndex) {
                            this.selected.splice(selectedIndex, 1);
                        }

                        ly.store.set("carts", this.carts);
                    }
                );
            },
        },
        components: {
            shortcut: () => import("/js/pages/shortcut.js")
        }
    })
</script>
<!-- 底部栏位 -->
<!--页面底部，由js动态加载-->
<script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
<div class="clearfix footer"></div>
<script type="text/javascript">$(".footer").load("foot.html");</script>
<!--页面底部END-->
<script type="text/javascript" src="js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="js/widget/nav.js"></script>

</body>

</html>